package com.st.buy.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.st.buy.content.service.ContentService;
import com.st.buy.manager.mapper.TbContentMapper;
import com.st.buy.pojo.TbContent;
import com.st.buy.util.json.JsonUtil;
import com.st.buy.util.pojo.BuyResult;
import com.st.buy.util.pojo.EasyUIDatagrideVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 张双虎
 */
@Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private TbContentMapper contentMapper;
    public BuyResult save(TbContent content) {
        content.setCreated(new Date());
        content.setUpdated(new Date());
        if(contentMapper.insert(content)==1){
            //清理缓存
            String key = "CONTENT_"+content.getCategoryId();
            stringRedisTemplate.delete(key);
            return BuyResult.ok();
        }
        return BuyResult.err5x();
    }

    @Override
    public EasyUIDatagrideVO<TbContent> queryByCid(long cid, long page, long rows) {
        Page<TbContent> contentPage = new Page<>();
        //设置每页大小
        contentPage.setSize(rows);
        //设置当前页码  内部会自动计算偏移量
        contentPage.setCurrent(page);
        //准备查询条件
        QueryWrapper<TbContent> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",cid);
        contentPage =  contentMapper.selectPage(contentPage,wrapper);
        //准备一个EasyUIDatagrideVO
        EasyUIDatagrideVO<TbContent> datagrideVO = new EasyUIDatagrideVO<>();
        datagrideVO.setTotal(contentPage.getTotal());
        datagrideVO.setRows(contentPage.getRecords());
        return datagrideVO;
    }

    @Override
    public List<TbContent> queryByCid(long cid) {
        //先准备一个key
        String key = "CONTENT_"+cid;
        //先查询缓存
        if(stringRedisTemplate.hasKey(key)){//如果key存在，就获取数据
            String json = stringRedisTemplate.opsForValue().get(key);
            //判断是否是空字符串
            if(StringUtils.isNoneBlank(json)){
                //更新存活时间
                stringRedisTemplate.expire(key,60*30, TimeUnit.SECONDS);
                //将json转换为列表返回
                return JsonUtil.json2list(json,TbContent.class);
            }
        }
        //如果缓存没有数据，就从数据库查询
        QueryWrapper<TbContent> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",cid);
        List<TbContent> contents = contentMapper.selectList(wrapper);
        //将结果放入缓存
        stringRedisTemplate.opsForValue().set(key,JsonUtil.object2json(contents));
        //设置key的存活时间 这里的具体时间可以考虑写在配置文件中。
        stringRedisTemplate.expire(key,60*30, TimeUnit.SECONDS);
        return contents;
    }
}
